import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * AddDisciplineWindow.java
 * 
 * Provides a window to add a discipline to the database
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AddDisciplineWindow extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = -1987273009263189086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	
	private AddDisciplineWindow self = this;
	private JPanel pane;
	private JPanel inputPane;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JTextField codeInput;
	private JTextArea descriptionInput;
	private JScrollPane descriptionScroll;
	private JTextArea punishmentInput;
	private JScrollPane punishmentScroll;
	
	private boolean added = false;
	
	/**
	 * Constructor
	 */
	public AddDisciplineWindow(JFrame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Add Discipline");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		inputPane = new JPanel();
		inputPane.setLayout(new GridLayout(2,1, HGAP, VGAP));
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		
		codeInput = new JTextField("Code", 20);
		descriptionInput = new JTextArea("Description", 3, 3);
		punishmentInput = new JTextArea("Punishment");
		descriptionInput.setLineWrap(true);
		punishmentInput.setLineWrap(true);
		descriptionScroll = new JScrollPane(descriptionInput);
		punishmentScroll = new JScrollPane(punishmentInput);
		
		inputPane.add(descriptionScroll);
		inputPane.add(punishmentScroll);
		
		selectBut = new JButton("Add");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		pane.add(codeInput, BorderLayout.NORTH);
		pane.add(inputPane, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method for ActionListener
	 * Used for select and cancel buttons
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut && codeInput.getText().length() > 0 &&
				!codeInput.getText().equals("Code") &&
				descriptionInput.getText().length() > 0 &&
				!descriptionInput.getText().equals("Description") &&
				punishmentInput.getText().length() > 0 &&
				!punishmentInput.getText().equals("Punishment")) {
			added = true;
			this.setVisible(false);
		}
		else if (e.getSource() == selectBut)
			JOptionPane.showMessageDialog(self, "Fields Must Have Valid Input", 
					"Discipline Addition Warning", JOptionPane.WARNING_MESSAGE);
		else if (e.getSource() == cancelBut)
			this.setVisible(false);
	}
	
	/**
	 * Returns the new Discipline information
	 * 
	 * @return	An Array of the new Discipline information
	 */
	public String[] getResult() {
		if(added) {
			String[] result = new String[3];
			result[0] = codeInput.getText();
			result[1] = descriptionInput.getText();
			result[2] = punishmentInput.getText();
			return result;
		}
		else 
			return null;
	}
}
